package com.fer.tankr.models.enemies;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.shape.IShape;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class FatGuyEnemy extends Enemy{

	private IShape toFollow;
	private Camera camera;
	
	public FatGuyEnemy(PhysicsWorld world, VertexBufferObjectManager vbom,
			float x, float y, IShape toFollow, Camera camera, ITextureRegion texture) {
		super(world, vbom, x, y, 50, 50, 8);
		this.toFollow = toFollow;
		this.camera = camera;
		attachSprite(texture, 0, 0);
		body.setAngularVelocity(3f);
	}
	
	@Override
	public Body initBody() {
		return PhysicsFactory.createCircleBody(physWorld, entity, BodyType.DynamicBody, fixtureDef);
	}
	
	@Override
	public void onUpdate(float pSecondsElapsed) {
		
		if (camera.isRectangularShapeVisible(entity))
		{
			entity.setColor((float)Math.random()/2,0,0);
			
			move();
		}
	}

	private void move() {
		float x = toFollow.getX()-entity.getX();
		float y = toFollow.getY()-entity.getY();
		Vector2 v = new Vector2(x, y).nor().mul(3.0f);
		body.setLinearVelocity(v);
	}


}
